package com.sinog.biz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @desc 线程池配置
 * @author lzq
 * @date -
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * 异步线程池
     * @return Executor
     */
    @Bean("taskAsync01-")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(50);
        //最大线程数
        executor.setMaxPoolSize(100);
        //缓冲队列
        executor.setQueueCapacity(500);
        //允许线程的空闲时间
        executor.setKeepAliveSeconds(1);
        //线程前缀
        executor.setThreadNamePrefix("taskAsync01-");
        //线程策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

    /**
     * 定时任务线程池(这个不自定义的话@Scheduled默认单线程，同时只能执行一个任务)
     * @return TaskScheduler
     */
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(30);
        return taskScheduler;
    }
}